import { Component, Injector, ViewChild } from '@angular/core';
import { MessageService } from '@abp/message/message.service';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { InspectionBoardServiceProxy, InspectionSeverityDataDto, InspectionDistributionDto, InspectionDistributionlistDto } from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import { finalize } from 'rxjs/operators';
import * as moment from 'moment';
import { EventManager } from '@angular/platform-browser';
@Component({
    selector: 'app-exception-kanban',
    templateUrl: './exception-kanban.component.html',
    styleUrls: ['./exception-kanban.component.less']
})
export class ExceptionKanbanComponent extends AppComponentBase {
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;

    @ViewChild('paginator') paginatorbg: Paginator;
    @ViewChild('dataTable') dataTablebg: Table;
    data2: any;
    inepectionBoard: InspectionSeverityDataDto;
    inspectionDistribution: InspectionDistributionDto[];
    inspectionDistributionList: any;
    date: any;
    week: any;
    time: any;
    timer: any;
    fullScreen = false;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _inspectionBoardServiceProxy: InspectionBoardServiceProxy,
        private eventManager: EventManager,

    ) {
        super(injector);

    }

    checkFull() {
        let full_status = document['fullscreen'] || document['webkitIsFullScreen'] || document['mozFullScreen'] || false;
        if (!full_status) {
            this.fullScreen = false;
        } else {
            this.fullScreen = true;
        }
    }
    full() {
        if (!this.fullScreen) {
            const docElmWithBrowsersFullScreenFunctions = document.getElementById('main') as HTMLElement & {
                webkitRequestFullscreen(): Promise<void>;
            };
            if (docElmWithBrowsersFullScreenFunctions.requestFullscreen) {
                docElmWithBrowsersFullScreenFunctions.requestFullscreen();
            } else if (docElmWithBrowsersFullScreenFunctions.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
                docElmWithBrowsersFullScreenFunctions.webkitRequestFullscreen();
            }
            this.fullScreen = true;
        } else {
            const docWithBrowsersExitFunctions = document as Document & {
                webkitExitFullscreen(): Promise<void>;
            };
            if (docWithBrowsersExitFunctions.exitFullscreen) {
                docWithBrowsersExitFunctions.exitFullscreen();
            } else if (docWithBrowsersExitFunctions.webkitExitFullscreen) { /* Chrome, Safari and Opera */
                docWithBrowsersExitFunctions.webkitExitFullscreen();
            }
            this.fullScreen = false;
        }
    }
    ngOnDestroy(): void {
        clearInterval(this.timer);
    }
    ngOnInit() {
        // this.eventManager.addGlobalEventListener('window','keyup.esc',()=>{
        //     this.checkFull();
        //   });
        this.getInepectionBoardTitleEv()
        this.getInspectionDistributionDtoEv()
        this.getInspectionDistributionlistEv()
        setInterval(() => {
            this.getInepectionBoardTitleEv()
            this.getInspectionDistributionDtoEv()
            this.getInspectionDistributionlistEv()
        }, 3600000);
    }
    ngAfterContentChecked() {
        //渲染完成后
        this.getNowDate();
        if (this.timer) {

        } else {
            this.timer = setInterval(this.getNowDate, 1000);
        }
    }
    getNowDate() {
        let nowDate = new Date();
        this.date = moment(nowDate).format('YYYY-MM-DD');
        var a = ["日", "一", "二", "三", "四", "五", "六"];
        var week = new Date().getDay();
        this.week = "星期" + a[week];
        this.time = moment(nowDate).format('HH:mm:ss');
    }
    //异常严重度分布
    getInepectionBoardTitleEv() {
        this._inspectionBoardServiceProxy.getInepectionBoardTitle()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.inepectionBoard = result
                this.data2 = {
                    labels: result.severity,
                    datasets: [
                        {
                            data: result.quantity,
                            backgroundColor: [
                                "#FF6384",
                                "#36A2EB",
                                "#FFCE56"
                            ],
                            hoverBackgroundColor: [
                                "#FF6384",
                                "#36A2EB",
                                "#FFCE56"
                            ]
                        }]
                };
                //    /// 严重度
                // /// </summary>
                // public List<string> Severity { get; set; }
                // /// <summary>
                // /// 数量
                // /// </summary>
                // public List<int> Quantity { get; set; }
            });
    }

    //当前异常分布/api/services/app/InspectionBoard/GetInspectionDistributionDto
    getInspectionDistributionDtoEv() {
        this._inspectionBoardServiceProxy.getInspectionDistributionDto()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.inspectionDistribution = result
            });
    }

    //当前异常/api/services/app/InspectionBoard/GetInspectionDistributionlist
    getInspectionDistributionlistEv() {
        this._inspectionBoardServiceProxy.getInspectionDistributionlist()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.inspectionDistributionList = result
                this.inspectionDistributionList.forEach(element => {
                    element.images = []
                    element.inspectionFileUploadDto.forEach(file => {
                        let obj = {
                            title: "",
                            thumbnail: '',
                            source: ""
                        }
                        obj.title = file.fileName
                        obj.source = file.fileUrl
                        obj.thumbnail = file.fileUrl
                        element.images.push(obj)
                    });
                });
            });
    }


}
